
#include "param.h"
#include <yaml-cpp/yaml.h>
#include <fstream>
/* prama define describe -----------------------------------------------------*/
/* class describe -----------------------------------------------------*/
namespace parameter
{
    Parameter::Parameter(std::string file_name)
    {
        config_file_ = file_name;
    }

    void Parameter::get_parameter( std::string param_name, std::string &param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            param_value = config[param_name].as<std::string>();
            std::cout<<"param_value:"<<param_value<<std::endl;
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter data type read error!" << std::endl;
        }
    }
    void Parameter::get_parameter( std::string param_name, int &param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            param_value = config[param_name].as<int>();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter data type read error!" << std::endl;
        }
    }

    void Parameter::get_parameter( std::string param_name, float &param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            param_value = config[param_name].as<float>();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter data type read error!" << std::endl;
        }
    }

   void Parameter::get_parameter( std::string param_name, double &param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            param_value = config[param_name].as<double>();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter data type read error!" << std::endl;
        }
    }

    void Parameter::get_parameter( std::string param_name, bool &param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            param_value = config[param_name].as<bool>();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter data type read error!" << std::endl;
        }
    }

    void Parameter::set_parameter( std::string param_name, std::string param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            std::ofstream config_stream(config_file_);
            config[param_name] = param_value;
            config_stream << config;
            config_stream.close();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter write error!" << std::endl;
        }
    }

    void Parameter::set_parameter( std::string param_name, int param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            std::ofstream config_stream(config_file_);
            config[param_name] = param_value;
            config_stream << config;
            config_stream.close();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter write error!" << std::endl;
        }
    }

    void Parameter::set_parameter( std::string param_name, float param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            std::ofstream config_stream(config_file_);
            config[param_name] = param_value;
            config_stream << config;
            config_stream.close();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter write error!" << std::endl;
        }
    }

    void Parameter::set_parameter( std::string param_name, double param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            std::ofstream config_stream(config_file_);
            config[param_name] = param_value;
            config_stream << config;
            config_stream.close();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter write error!" << std::endl;
        }
    }

    void Parameter::set_parameter( std::string param_name, bool param_value)
    {
        YAML::Node config;
        try
        {
            config = YAML::LoadFile(config_file_);
        }
        catch (YAML::BadFile &e)
        {
            std::cout << config_file_ << ": file read error!" << std::endl;
            return;
        }

        try
        {
            std::ofstream config_stream(config_file_);
            config[param_name] = param_value;
            config_stream << config;
            config_stream.close();
        }
        catch (YAML::BadConversion &e)
        {
            std::cout << param_name << ": parameter write error!" << std::endl;
        }
    }
}

/************************ (.cpp) END OF FILE ************************************/
